package com.cvtt.xmpp;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;

public class XMPPPacketListener implements PacketListener {
	
	private XMPPEngine xmppManager;
	private static final String TAG="XMPPPacketListener";
	private PacketFilter allPacketFilter;
	
	public XMPPPacketListener(XMPPEngine xmppManager) {
		// TODO Auto-generated constructor stub
		this.xmppManager = xmppManager;
		
		allPacketFilter = new PacketFilter() {

			@Override
			public boolean accept(Packet packet) {
				// TODO Auto-generated method stub
			    return true;
			}
		};
	}
	
	public void registerPacketListener(XMPPConnection xmppConn)
	{
		if(xmppConn == null)
			return;
		xmppConn.addPacketListener(this, allPacketFilter);
	}
	
	public void removePacketListener(XMPPConnection xmppConn)
	{
		if(xmppConn == null)
			return;
		xmppConn.removePacketListener(this);
	}

	@Override
	public void processPacket(Packet packet) {

		// TODO Auto-generated method stub
		if (packet instanceof org.jivesoftware.smack.packet.RosterPacket) 
		{
			xmppManager.processRosterPacket(packet);
		}
		else if(packet instanceof org.jivesoftware.smack.packet.Message)
		{
			xmppManager.processMessagePacket(packet);
		}
		else if (packet instanceof org.jivesoftware.smack.packet.Presence) 
		{
			Presence pres = (Presence) packet;
	    	if (pres.getType() == Presence.Type.subscribe)
	    	{
	    		xmppManager.processSubscribePacket(packet);
	    	}
		}
		else
		{
			xmppManager.processOtherPacket(packet);
		}
	}
}
